package com.leesche.zcsp.node.web.controller.wechat.operate;

import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubPlace;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.PubPlaceService;
import com.leesche.zcsp.node.web.service.impl.data.read.InfoDeviceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.PubPlaceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceCondition;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubPlaceCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import com.leesche.zcsp.node.web.vo.Principal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by kaider on 2016/10/18.
 */
@Controller
@RequestMapping("/wechat/operate/place")
public class WechatPlaceController {
	private final static Logger LOGGER = LoggerFactory.getLogger(WechatPlaceController.class);

	@Autowired
	private PubPlaceService placeService;
	@Autowired
	private PrivilegeService privilegeService;
	@Autowired
	private PubDeviceService deviceService;

	private final static String LIST_VIEW = "/wechat/operate/place/listView";
	private final static String ADD_VIEW = "/wechat/operate/place/addView";
	private final static String SHOW_PLACE_VIEW = "/wechat/operate/place/showView";
	private final static String EDIT_VIEW = "/wechat/operate/place/editView";

	private final static String FINANCE_MACHINE_VIEW = "/wechat/operate/place/financeMachineView";

	@RequestMapping(value = "/listView", method = RequestMethod.GET)
	public String listView(Model model) {
		Long adminId = getAdminId();
		Integer userRoleType = privilegeService.getCurrentPrincipal().getNtype();

		QueryPubPlaceCondition condition = new QueryPubPlaceCondition();
		condition.setAdminId(adminId);
		List<PubPlaceDto> list = placeService.findPubPlaceList(condition);
		model.addAttribute("list", list);
		model.addAttribute("cur", "place");

		String goView;

		switch (userRoleType){
			//机修
			case 6:
				goView = FINANCE_MACHINE_VIEW;
				model.addAttribute("financeMachine",0);
				break;
			//财务兼机修
			case 7:
				goView = FINANCE_MACHINE_VIEW;
				model.addAttribute("financeMachine",1);
				break;
			default:
				goView = LIST_VIEW;
				break;
		}

		return goView;
	}

	@RequestMapping(value = "/addView", method = RequestMethod.GET)
	public String addView() {
		return ADD_VIEW;
	}

	/*@RequestMapping(value = "/addView", method = RequestMethod.GET)
	public String addView(Model model) {
		QueryPubPlaceTypeCondition condition = new QueryPubPlaceTypeCondition();
		condition
				.setAdminId(privilegeService.getCurrentPrincipal().getUserid());
		List<PubPlaceTypeDto> list = placeTypeService
				.findPlaceTypeList(condition);
		model.addAttribute("list", list);
		return ADD_VIEW;
	}*/

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String, Object> add(PubPlace place) {
		try {
			Long adminId = getAdminId();
			place.setUserid(privilegeService.getCurrentPrincipal().getUserid());
			place.setAdminid(adminId);
			placeService.insert(place);
			return WebResult.success().info("操作成功").toMap();
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return WebResult.failure(e).toMap();
		}
	}

	@RequestMapping(value = "/{id}/showView/{index}", method = RequestMethod.GET)
	public String showView(@PathVariable("id") Long placeId,
			@PathVariable("index") Integer index, Model model) {
		PubPlace place = placeService.selectByPrimaryKey(placeId);
		QueryPubDeviceCondition condition = new QueryPubDeviceCondition();
		condition.setAdminId(place.getAdminid());
		condition.setPlaceId(placeId);
		List<InfoDeviceDto> deviceList = deviceService.findDeviceList(condition);
		model.addAttribute("place", place);
		model.addAttribute("index", index);
		model.addAttribute("deviceList", deviceList);
		return SHOW_PLACE_VIEW;
	}

	@RequestMapping(value = "/{id}/editView", method = RequestMethod.GET)
	public String editView(@PathVariable("id") Long placeId, Model model) {
		PubPlace place = placeService.selectByPrimaryKey(placeId);
		model.addAttribute("place", place);
		return EDIT_VIEW;
	}

	@ResponseBody
	@RequestMapping(value = "/{placeId}/edit", method = RequestMethod.POST)
	public Map<String, Object> edit(@PathVariable("placeId") Long placeId,
			String address) {
		try {
			Long adminId = getAdminId();

			PubPlace place = placeService.selectByPrimaryKey(placeId);
			place.setUserid(privilegeService.getCurrentPrincipal().getUserid());
			place.setAdminid(adminId);
			place.setAddress(address);
			placeService.updateByPrimaryKeySelective(place);
			return WebResult.success().info("操作成功").toMap();
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return WebResult.failure(e).toMap();
		}
	}

	@ResponseBody
	@RequestMapping(value = "/checkUserPlace", method = RequestMethod.GET)
	public Map<String,Object> checkUserPlace(){
		Long adminId = getAdminId();
		QueryPubPlaceCondition condition = new QueryPubPlaceCondition();
		condition.setAdminId(adminId);
		List<PubPlaceDto> placeList = placeService.findPubPlaceList(condition);
		boolean flag=false;
		if(placeList.size()>0){
			flag = true;
		}
		return WebResult.success().put("placeBoolean",flag).toMap();
	}

	private Long getAdminId(){
		Principal principal = privilegeService.getCurrentPrincipal();
		Long adminId;
		if(principal.getParentid()==1){
			adminId = principal.getUserid();
		}else{
			adminId = principal.getParentid();
		}
		return adminId;
	}
}
